package fr.n7.khome.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;

public class JRequestField extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1042431893700167411L;
	protected double displayedProportion = 0.7;
	protected double heightProportion = 0.7;
	
	protected int w0;
	protected int w;
	protected int h0;
	protected int h;
	
	protected String text;

	public JRequestField(){
		text = "";
	}
	
	public void initializePaintComponent(Graphics g){
		w0 = (int)(0.02*getWidth()/2);
		w = (int)(getWidth()-2*w0);
		h0 = (int)(0.02*getHeight()/2);
		h = (int)(getHeight()-2*h0);
		g.setColor(Color.black);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(new Color(150,150,200));
		g.fillRect(1, 1, getWidth()-2, getHeight()-2);
	}
	
	@Override
	public void paintComponent(Graphics g){
		initializePaintComponent(g);
		g.setFont(new Font(g.getFont().getFontName(),Font.BOLD,13));
		drawLabel(w/2, g, " "+text+" ", h0+h/2, -.5f,  new Color(220 ,220 ,220), 5);
	}
	
	public void setText(String t){
		this.text = t;
		repaint();
	}
	
	public void drawTopBottomLabel(int x, Graphics g, String string, boolean top){
		drawTopBottomLabel(x, g, string, top, new Color(255,255,255,200));
	}

	public void drawTopBottomLabel(int x, Graphics g, String string, boolean top, Color color){
		Rectangle2D r = g.getFontMetrics().getStringBounds(string, g).getFrame();
		drawLabel(x, g, string, top?0:(2*h0+h-(int)r.getHeight()), 0f, color, 0);
	}

	public void drawLabel(int x, Graphics g, String string, int height, float HeightFactor, Color color, int spaceToBorder){

		Rectangle2D r = g.getFontMetrics().getStringBounds(string, g).getFrame();
		height = height + (int)(HeightFactor*r.getHeight());
		int sx = Math.min(w0+w-(int)r.getWidth(),Math.max(w0,w0+x+(int)r.getMinX()-(int)(r.getWidth()/2)));
		int sy = height;
		//(equal?(h0+h/2):(h0+h))-(int)r.getHeight();
		int sw = (int)r.getWidth();
		int sh = (int)r.getHeight();

		g.setColor(color);

		g.fillRoundRect(sx-spaceToBorder, sy-spaceToBorder, sw+2*spaceToBorder, sh+2*spaceToBorder, 10, 3);

		g.setColor(Color.black);
		g.drawRoundRect(sx-spaceToBorder, sy-spaceToBorder, sw+2*spaceToBorder, sh+2*spaceToBorder, 10, 3);

		g.drawString(string, sx, sy+(int)g.getFontMetrics().getAscent());
	}
}
